//
//  CollectionView.m
//  ImageView
//
//  Created by Apple on 1/25/16.
//  Copyright © 2016 Apple. All rights reserved.
//

#import "CollectionView.h"
#import "CollectionViewCell.h"
@implementation CollectionView

- (void)awakeFromNib {
    // Initialization code
    UINib *headNib = [UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
    [self.collection registerNib:headNib forCellWithReuseIdentifier:@"CollectionViewCell"];
    self.collection.scrollEnabled=NO;
    self.collection.backgroundColor=[UIColor whiteColor];
    self.collection.delegate=self;
    self.collection.dataSource=self;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionViewCell *cell = (CollectionViewCell *)(UICollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
    cell.collectionCell.image=[UIImage imageNamed:@"1.jpg"];
//    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Transform:)];
//    cell.collectionCell.userInteractionEnabled=YES;
//    [cell.collectionCell addGestureRecognizer:tap];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UIView *bg=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,  [UIScreen mainScreen].bounds.size.height)];
    
    self.view=nil;
    self.origin=nil;
    self.rect=CGRectMake(0, 0, 0, 0);
    self.origin=[collectionView cellForItemAtIndexPath:indexPath];
    
    UIView *v3=self.superview.superview.superview;
    
    CGRect rect3=[bg convertRect:self.origin.frame fromView:self.origin.superview];
    
    self.origin.hidden=YES;
    self.rect=CGRectMake(rect3.origin.x, rect3.origin.y, rect3.size.width, rect3.size.height);
    
    self.view= [[UIImageView alloc]initWithFrame:self.rect];
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoClick:)];
    [self.view addGestureRecognizer:singleTap];
    self.view.userInteractionEnabled=YES;
    
    self.view.image=[UIImage imageNamed:@"1.jpg"];
    //view.backgroundColor=[UIColor redColor];
    [v3 addSubview:self.view];
    [UIView animateWithDuration:0.4f animations:^{
        self.superview.superview.hidden=YES;
        
        self.view.frame=CGRectMake(0, [UIScreen mainScreen].bounds.size.height/2.0-100, [UIScreen mainScreen].bounds.size.width,200);
    } completion:^(BOOL finished) {
        //_imageView.frame=CGRectMake(0, [UIScreen mainScreen].bounds.size.height/2.0-100, [UIScreen mainScreen].bounds.size.width,200);
    }];
}

-(void)Transform:(UITapGestureRecognizer *)tap{
    self.view=nil;
    self.origin=nil;
    self.rect=CGRectMake(0, 0, 0, 0);
    self.origin=tap.view;
    
    UIView *v3=self.superview.superview.superview;
    
    CGRect rect3=[self convertRect:tap.view.frame fromView:tap.view.superview];
    
    self.origin.hidden=YES;
    self.rect=CGRectMake(rect3.origin.x, rect3.origin.y+64, rect3.size.width, rect3.size.height);
    
    self.view= [[UIImageView alloc]initWithFrame:self.rect];
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoClick:)];
    [self.view addGestureRecognizer:singleTap];
    self.view.userInteractionEnabled=YES;
    
    //self.view.image=self.origin.image;
    //view.backgroundColor=[UIColor redColor];
    [v3 addSubview:self.view];
    [UIView animateWithDuration:0.4f animations:^{
        self.superview.superview.hidden=YES;
        
        self.view.frame=CGRectMake(0, [UIScreen mainScreen].bounds.size.height/2.0-100, [UIScreen mainScreen].bounds.size.width,200);
    } completion:^(BOOL finished) {
        //_imageView.frame=CGRectMake(0, [UIScreen mainScreen].bounds.size.height/2.0-100, [UIScreen mainScreen].bounds.size.width,200);
    }];
}

-(void)photoClick:(id)sender{
    [UIView animateWithDuration:0.4f animations:^{
        self.superview.superview.hidden=NO;
        self.view.frame=self.rect;
    } completion:^(BOOL finished) {
        self.view.hidden=YES;
        self.origin.hidden=NO;
        //_imageView.frame=CGRectMake(0, [UIScreen mainScreen].bounds.size.height/2.0-100, [UIScreen mainScreen].bounds.size.width,200);
    }];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _Number.intValue;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((self.collection.frame.size.width-16)/3.0,(self.collection.frame.size.width-16)/3.0);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return  UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 8;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 8;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
